package com.suse.lhw.hms.authserver.controller;

import com.suse.lhw.hms.authserver.config.UserDetailsServiceImpl;
import com.suse.lhw.hms.authserver.domin.UUser;
import com.suse.lhw.hms.authserver.service.UUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

/**
 * @ProjectName: health_manage_system
 * @Package: com.suse.lhw.hms.authserver.controller
 * @ClassName: LoginAndRejesterController
 * @Description: java类作用描述
 * @Author: 李鸿炜
 * @CreateDate: 2020/4/2 11:18
 * @UpdateUser: 更新者
 * @UpdateDate: 2020/4/2 11:18
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
@RestController
public class LoginAndRejesterController {
    @Autowired
    UUserService serService;
    @Autowired
    BCryptPasswordEncoder bCryptPasswordEncoder;

    @PostMapping("/my-login")
    public String myLogin(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String name, String password) {
        String username = httpServletRequest.getParameter("name");
        System.out.println("the data from request is " + username);
        System.out.println("the intput is :name-" + name + " and the password is " + password);
        System.out.println("the encoded psd is " + bCryptPasswordEncoder.encode(password));
        UUser uUser = serService.loginByPrimarryKeyAndPsd(name);
        if (uUser == null) {
            System.out.println("沒有找到這個賬號哦");
            httpServletRequest.setAttribute("msg", "没有这个账号喔！");
        } else {
            if (bCryptPasswordEncoder.matches(password, uUser.getPassword())) {
                System.out.println("登录成功");
                httpServletRequest.setAttribute("msg", "登录成功！");
                return "/hello";
            } else {
                System.out.println("登录失败");
                httpServletRequest.setAttribute("msg", "密码和账号不对应！");
            }
        }
        return null;

    }
}
